#include "array.h"

sizeLink* newSizeLink(int size)
{
    sizeLink* ret = (sizeLink*)malloc(sizeof(sizeLink));
    ret->size = size;
    ret->next = NULL;
    return ret;
}

void freeSizeLink(sizeLink* head)
{
    while(head)
    {
        sizeLink* tmp = head;
        head = head->next;
        free(tmp);
    }
}

arrayTs* newArrayTs(int dim, sizeLink* sl, bool para)
{
    arrayTs* ret = (arrayTs*)malloc(sizeof(arrayTs));
    ret->dim = dim;
    ret->size = (int*)malloc(sizeof(int)*dim);
    ret->width = (int*)malloc(sizeof(int)*dim);
    int dimSize = 1;
    sizeLink* tmp = sl;
    for(int i = dim-1; i >= 0; i--)
    {
        ret->size[i] = dimSize;
        ret->width[i] = tmp->size;
        dimSize *= tmp->size;
        tmp = tmp->next;
    }
    ret->para = para;
    freeSizeLink(sl);
    return ret;
}

void freeArrayTs(arrayTs* target)
{
    free(target->size);
    free(target);
}

void printat(arrayTs* at)
{
    for(int i = 0;i<at->dim;i++)
    {
        DEBUG("%d %d %d\n",i,at->width[i],at->size[i]);
    }
}